<template>
  <div>
    <el-form :model="form" label-width="80px">
      <el-form-item label="账号">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
    }
  },
  created() {},
  mounted() {},
  watch: {},
  computed: {},
  methods: {
    ...mapMutations(['set_token']),
    async submitForm() {
      if (this.form.username && this.form.password) {
        const res = await axios.post('/api/login', this.form)
        const token = res.data.data.token
        if (res.data.code === 0) {
          localStorage.setItem('token', token)
          this.set_token(token)
          const fullpath = this.$route.query.fullpath || '/base/home'
          this.$message.success('登陆成功，即将跳转')
          this.$router.push(fullpath)
        } else {
          this.$message.success('登陆失败，请重试')
        }
      }
    },
  },
}
</script>

<style></style>
